import {
    Action
} from 'redux';

const initState = {
    list:[]
}

import * as types from './../types/discover';
import * as commonTypes from './../types/common';

interface _Action extends Action{
    data:any
}

export default (
    state:any=initState,
    action:_Action
) => {

    const {
        type,
        data
    } = action;

    switch (type) {

        case types.getDiscoverList:{
            state.list = data;
            return {
                ...state
            }
        }

        case commonTypes.postLikeUser:{
            const {
                userId
            } = data;

            const {
                list
            } = state;

            const index = list.findIndex((item: any) => {
                return item.userId === userId;
            })

            if (index > -1) {
                (list[index] as any).isLiked = 1;

                return {
                    ...state,
                    list: [...list]
                }
            }
        }

        case commonTypes.postUnlikeUser:{
            const {
                userId
            } = data;

            const {
                list = []
            } = state;

            const index = list.findIndex((item: any) => {
                return item.userId === userId;
            })

            if (index > -1) {
                (list[index] as any).isLiked = -1;

                return {
                    ...state,
                    list: [...list]
                }
            }
        }

        case commonTypes.resetLikeUser: {
            const {
                userId
            } = data;

            const {
                list = []
            } = state;

            const index = list.findIndex((item: any) => {
                return item.userId === userId;
            })

            if (index > -1) {
                (list[index] as any).isLiked = 0;

                return {
                    ...state,
                    list: [...list]
                }
            }
        }
    }

    return state;
}